package cn.itsource.quartz.job;

import cn.itsource.order.domain.OrderAdopt;
import cn.itsource.order.service.IOrderAdoptService;
import cn.itsource.pay.contants.BusinessContants;
import cn.itsource.pay.domain.PayBill;
import cn.itsource.pay.service.IPayBillService;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.service.IPetService;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
public class MainJob implements Job {

    @Autowired
    private IPayBillService payBillService;
    @Autowired
    private IOrderAdoptService orderAdoptService;

    @Autowired
    private IPetService petService;

    //写 取消订单的业务逻辑
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //1.获取定时任务里面的参数【orderSn】
        //获取任务详情，获取数据
        JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        // params   orderSn
        Map<String,Object> params = (Map<String, Object>) jobDataMap.get("params");
        //获取map里面的值
        String  orderSn = (String) params.get("orderSn");
        //2.根据orderSn 查询 PayBill
        PayBill payBill =payBillService.queryByOrderSn(orderSn);
        //3.把PayBill订单的状态设置为-1 更新时间
        payBill.setState(-1);//取消订单
        payBill.setUpdateTime(new Date());
        payBillService.update(payBill);

        //2.找到对应的订单数据
        switch (payBill.getBusinessType()){
            case BusinessContants.ORDER_ADOPT:
                //4.根据PayBill业务类型找到 订单表【t_order_adopt】
                OrderAdopt orderAdopt= orderAdoptService.queryByOrderSn(orderSn);
                //5.修改订单状态为-1
                orderAdopt.setState(-1);//取消订单
                orderAdoptService.update(orderAdopt);
                //6.根据订单里面宠物id重新上架宠物
                Pet pet = petService.loadById(orderAdopt.getPet_id());
                pet.setState(1);
                pet.setOnsaletime(new Date());
                //更新数据
                petService.update(pet);

                break;
            case BusinessContants.ORDER_PRODUCT:
                //服务订单

                break;
            case  BusinessContants.ORDER_GOODS:
                //商品订单

                break;

        }






    }
}
